/**
  ******************************************************************************
  * @file   :   .c
  * @version:
  * @author :
  * @brief  :
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright(c) . BLUEX Microelectronics.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */

/* includes ------------------------------------------------------------------*/

#include "bx_kernel.h"
#include "user_app.h"
#include "user_service_module_QueryMac.h"
#include "user_service_module_reboot.h"
#include "user_service_module_NextCommand.h"
#include "log.h"

/* private define ------------------------------------------------------------*/

/* private typedef -----------------------------------------------------------*/
struct us_QueryMac_service {
    s32 id;
    
};
/* private variables ---------------------------------------------------------*/
static struct us_QueryMac_service QueryMac_svc = {0};
/* exported variables --------------------------------------------------------*/

/* private macros ------------------------------------------------------------*/
#define GET_QueryMac_SERVICE_BY_ID( p_svc, svc_id )                  \
do{                                                             \
    if( ( svc_id ) == QueryMac_svc.id ) {                            \
        p_svc = &QueryMac_svc;                                       \
    } else {                                                    \
        return BX_ERR_NOTSUP;                                   \
    }                                                           \
}while(0)

/*============================= private function =============================*/
extern s32 user_service_id;
/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
#define QueryMac_CMDLEN 5
static u8 send_char[QueryMac_CMDLEN] = {0xAA,0x55,0x02,0x00,0x02};
extern void uart0_send_buff(u8 * buf, u32 size);

static bx_err_t QueryMac_msg_handle(s32 svc_id, u32 msg, u32 param0, u32 param1 )
{
    struct us_QueryMac_service * p_svc;
    GET_QueryMac_SERVICE_BY_ID( p_svc, svc_id );
    
    switch( msg ) {
        case USM_MODULE_QueryMac_START:
            set_cmd_id(BXM_MODULE_CID_QueryMac);
            //send uart cmd
            uart0_send_buff(&send_char[0], QueryMac_CMDLEN);
            bx_post( user_service_id, BXM_USER_TIMER_START, 600, 0 );
            break;
        
        case BXM_USER_TIMER_TIMEOUT:
            if ( check_cmd_id(BXM_MODULE_CID_QueryMac) )
            {
                LOG_RAW("<< Query MAC timeout! svc id: 0x%x, msg: 0x%x, p0:%d,p1:%d\n",svc_id, msg, param0, param1 );

                bx_post( us_module_reboot_id(), USM_MODULE_REBOOT_START, 0, 0 );//timeout then reboot
            }
            break;

        case BXM_USER_CMD_RSP_READY:
        {
            if ( check_cmd_id(BXM_MODULE_CID_QueryMac) )
            {
                LOG_RAW("## Query MAC, msg:CMD RSP READY! svc id: 0x%x, msg: 0x%x, p0:%d,p1:%d\n",svc_id, msg, param0, param1 );
                u8 * p = (u8 *)param0;
                for ( u32 i=0; i<param1; i++)
                {
                    LOG_RAW("0x%x ", *p++);
                }
                LOG_RAW("\n\n");

                //rsp ok, then stop timeout timer
                bx_post( user_service_id, BXM_USER_TIMER_STOP, 0, 0 );
                //link next command
                bx_post( us_module_NextCommand_id(), USM_MODULE_NextCommand_START, 0, 0 );
            }
        }
        break;
        default:
            return BX_ERR_NOTSUP;
    }
    return BX_OK;
}

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
static bx_err_t QueryMac_property_set(s32 svc_id, u32 property, u32 param0, u32 param1 )
{
    struct us_QueryMac_service * p_svc;
    GET_QueryMac_SERVICE_BY_ID( p_svc, svc_id );
    
    switch( property ) {
        default:
            return BX_ERR_NOTSUP;
    }
}

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
static bx_err_t QueryMac_property_get(s32 svc_id, u32 property, u32 param0, u32 param1 )
{
    struct us_QueryMac_service * p_svc;
    GET_QueryMac_SERVICE_BY_ID( p_svc, svc_id );
    
    switch( property ) {
        default:
            return BX_ERR_NOTSUP;
    }
}


/*========================= end of private function ==========================*/


/*============================= exported function ============================*/

/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
bool us_QueryMac_register( void )
{
    struct bx_service svc;
    svc.name = "QueryMac service";
    svc.msg_handle_func = QueryMac_msg_handle;
    svc.prop_get_func = QueryMac_property_get;
    svc.prop_set_func = QueryMac_property_set;
    QueryMac_svc.id = bx_register( &svc );
    if( QueryMac_svc.id == -1 ) {
        return false;
    }
    return true;
}
/** ---------------------------------------------------------------------------
 * @brief   :
 * @note    :
 * @param   :
 * @retval  :
-----------------------------------------------------------------------------*/
s32 us_QueryMac_id( void )
{
    return QueryMac_svc.id;
}

/*========================= end of exported function =========================*/


/*============================= import function ==============================*/

/*=========================== end of import function =========================*/


/*============================ interrupt function ============================*/

/*========================= end of interrupt function ========================*/


/******************** (C) COPYRIGHT BLUEX **********************END OF FILE****/



